using System;
using System.Globalization;
using UnityEngine;

namespace Framework.Core
{
    public class FrameworkCore
    {
        private GameObject _behaviour;
        public Timer Timer { get; private set; }
        public CoroutineManager Coroutine { get; private set; }
        public Action<float, float> OnUpdate;
        public Action<float, float> OnFixedUpdate;
        public Action OnApplicationQuit;
        public Action<bool> OnApplicationFocus;
        public Action<bool> OnApplicationPause;
        public FrameworkCore(GameObject value = null)
        {
            CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");
            Timer = new Timer();

            if (value != null)
            {
                var behaviour = value.AddComponent<FrameworkCoreBehaviour>();
                if (behaviour == null)
                {
                    FrameworkLog.LogError("can't Initialize");
                    return;
                }
                behaviour.core = this;
                Coroutine = new CoroutineManager(value.transform);
            }
            else
            {
                FrameworkLog.LogError("需要调用 FrameworkCore Update方法");
            }
            // QualitySettings.vSyncCount = 1;
            // Application.targetFrameRate = 120;
        }
        public void Update(float deltaTime, float unscaledDeltaTime)
        {
            Timer.Update(deltaTime, unscaledDeltaTime);
            EventDispatcherAsync.Update(deltaTime, unscaledDeltaTime);
            OnUpdate?.Invoke(deltaTime, unscaledDeltaTime);
            FrameworkLog.Update();
        }
        public void FixedUpdate(float deltaTime, float unscaledDeltaTime)
        {
            OnFixedUpdate?.Invoke(deltaTime, unscaledDeltaTime);
        }
        [DisallowMultipleComponent]
        internal class FrameworkCoreBehaviour : MonoBehaviour
        {
            public FrameworkCore core;
            private void Update()
            {
                core?.Update(Time.deltaTime, Time.unscaledDeltaTime);
            }

            private void OnDisable()
            {
                if (core == null) return;
                FrameworkLog.LogErrorFormat("FrameworkCore节点{0}不可用", this.name);
            }
            private void FixedUpdate()
            {
                core?.FixedUpdate(Time.fixedDeltaTime, Time.fixedUnscaledDeltaTime);
            }
            private void OnApplicationFocus(bool focus)
            {
                if (core != null && core.OnApplicationFocus != null)
                {
                    core.OnApplicationFocus(focus);
                }
            }
            private void OnApplicationPause(bool pause)
            {
                if (core != null && core.OnApplicationPause != null)
                {
                    core.OnApplicationPause(pause);
                }
            }
            private void OnApplicationQuit()
            {
                if (core != null && core.OnApplicationQuit != null)
                {
                    core.OnApplicationQuit();
                }
                core = null;
            }
        }
    }
}
